<?php
/**
 * Created by PhpStorm.
 * User: qilin
 * Date: 2018/1/30
 * Time: 16:29
 */

namespace App\ServiceImpl;


use App\Model\WareHouse;
use App\Model\WareHouseIn;
use App\Model\WareHouseOut;
use App\Service\WareHouseService;
use Illuminate\Support\Facades\Validator;


class WareHouseServiceImpl implements WareHouseService
{
    public function getWareHouseList()
    {
        $data = WareHouse::where('warehouse_delete',0)->paginate(15)->toArray();
        return $data;
        // TODO: Implement getWareHouseService() method.
    }

    public function addWareHouse($wareHouse)
    {
        $validator = Validator::make($wareHouse,[
            'name'=>'required',
            'inPrice'=>'required',
            'outPrice'=>'required',
            'count'=>'required'
        ],[
            'name.required'=>'物品的名称为必填',
            'inPrice.required'=>'物品的进价为必填',
            'outPrice.required'=>'物品的售价为必填',
            'count.required'=>'物品数量为必填'
        ]);
        if($validator->fails()){
            $error = current((array)json_decode(json_encode($validator->errors())));
            return view("addWareHouse")->with('error',$error[0]);
        }
        $wareHouseId=WareHouse::insertGetId(['warehouse_name'=>$wareHouse['name'],'warehouse_in_price'=>$wareHouse['inPrice'],'warehouse_out_price'=>$wareHouse['outPrice']]);
        $wareHouseIn = new WareHouseIn;
        $wareHouseIn->warehouse_in_warehouse_id = $wareHouseId;
        $wareHouseIn->warehouse_in_count = $wareHouse['count'];
        $wareHouseIn->save();
        // TODO: Implement addWareHouse() method.
    }

    public function getWareHouseInList()
    {
        $data = WareHouseIn::join('warehouse','warehouse_in.warehouse_in_warehouse_id','=','warehouse.warehouse_id')->paginate(15);
        return $data;
        // TODO: Implement getWareHouseInList() method.
    }

    public function verifyWareHouseIn($id)
    {
        $wareHouseInData = WareHouseIn::where('warehouse_in_id',$id)->first()->toArray();
        $data  = WareHouse::where('warehouse_id',$wareHouseInData['warehouse_in_warehouse_id'])->first()->toArray();
        $count = $data['warehouse_count']+$wareHouseInData['warehouse_in_count'];
        if($count>=0){
            WareHouse::where('warehouse_id',$wareHouseInData['warehouse_in_warehouse_id'])->update(['warehouse_count'=>$count]);
            WareHouseIn::where('warehouse_in_id',$id)->update(['warehouse_in_verify'=>1]);
        }
        // TODO: Implement verifyWareHouseIn() method.
    }

    public function wareHouseIn($info)
    {
        $wareHouseIn = new WareHouseIn;
        $wareHouseIn->warehouse_in_warehouse_id = $info['id'];
        $wareHouseIn->warehouse_in_count = $info['count'];
        $wareHouseIn->save();
        // TODO: Implement wareHouseIn() method.
    }

    public function wareHouseOut($info)
    {
        $wareHouseOut = new WareHouseOut();
        $wareHouseOut->warehouse_out_warehouse_id = $info['id'];
        $wareHouseOut->warehouse_out_count = $info['count'];
        $wareHouseOut->save();
        // TODO: Implement wareHouseOut() method.
    }

    public function getWareHouseOutList()
    {
        $data = WareHouseOut::join('warehouse','warehouse_out.warehouse_out_warehouse_id','=','warehouse.warehouse_id')->paginate(15)->toArray();
        return $data;
        // TODO: Implement getWareHouseOutList() method.
    }

    public function verifyWareHouseOut($id)
    {
        $wareHouseOutData = WareHouseOut::where('warehouse_out_id',$id)->first()->toArray();
        $data  = WareHouse::where('warehouse_id',$wareHouseOutData['warehouse_out_warehouse_id'])->first()->toArray();
        $count = $data['warehouse_count']-$wareHouseOutData['warehouse_out_count'];
        if($count>=0){
            WareHouse::where('warehouse_id',$wareHouseOutData['warehouse_out_warehouse_id'])->update(['warehouse_count'=>$count]);
            WareHouseOut::where('warehouse_out_id',$id)->update(['warehouse_out_verify'=>1]);
        }
        // TODO: Implement verifyWareHouseOutList() method.
    }

}